package com.chmingx.admin.exception;

import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Order(value = Ordered.HIGHEST_PRECEDENCE) // 可以指定自定义异常处理器的优先级
@Component
public class CustomerHandlerExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        try {
            httpServletResponse.sendError(511, "自定义异常处理");
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }

        return new ModelAndView(); // 异常处理需要返回一个ModelAndView
//        return new ModelAndView("5xx.html"); // 异常处理需要返回一个ModelAndView, 可以指定一个页面

    }
}
